package com.zhengtoudai.ui.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.zhengtoudai.R;
import com.zhengtoudai.ui.bean.BorrowDetails;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/9/5 0005.
 */

public class RedPacketAdapter extends BaseAdapter {
    private List<BorrowDetails.RedListEntity> data;
    private LayoutInflater mInflater;
    private int checkPosition=0;
    private Context mContext;
    public RedPacketAdapter(LayoutInflater mInflater,Context mContext) {
        data=new ArrayList<>();
        BorrowDetails.RedListEntity entity=new BorrowDetails.RedListEntity();
        entity.setActivity_name("--选择红包--");
        data.add(entity);
        this.mInflater = mInflater;
        this.mContext=mContext;
    }

    public void addData(List<BorrowDetails.RedListEntity> data){
        this.data.addAll(data);
        notifyDataSetChanged();
    }

    public void clear(){
        data.clear();
        notifyDataSetChanged();
    }

    public List<BorrowDetails.RedListEntity> getData(){
        return data;
    }

    public void setCheckPosition(int position){
        checkPosition=position;
        notifyDataSetChanged();
    }

    public int getCheckPosition(){
        return checkPosition;
    }
    @Override
    public int getCount() {
        return data == null ? 0 : data.size();
    }

    @Override
    public Object getItem(int position) {
        return data == null ? position : data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView==null){
            convertView=mInflater.inflate(R.layout.popup_list_item, null);
            holder=new ViewHolder(convertView);
            holder.mTvContent= (TextView) convertView.findViewById(R.id.tv_content);
            convertView.setTag(holder);
        }else{
            holder= (ViewHolder) convertView.getTag();
        }
        if (position==0){
            holder.mTvContent.setText(data.get(position).getActivity_name());
        }else{
            holder.mTvContent.setText(data.get(position).getRed_money()+"元(满"+data.get(position).getMin_money()+"元可用)");
        }
        if (position==checkPosition){
            holder.mTvContent.setTextColor(ContextCompat.getColor(mContext,R.color.title_text));
        }else{
            holder.mTvContent.setTextColor(ContextCompat.getColor(mContext,R.color.black_3));
        }
        return convertView;
    }


    static class ViewHolder {
        @Bind(R.id.tv_content)
        TextView mTvContent;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
